package com.uwin.findTomcat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Hello world!
 *
 */
public class App 
{
	
	private static String CONF_NAME = "conf.json";
    public static void main( String[] args ) throws IOException
    {
    	/*RemoteExecuteCommand rec=new RemoteExecuteCommand("192.168.12.100", "root","*****");
    	
    	Free free = Free.reslvos(rec);
    	
    	System.out.println(JSON.toJSONString(free));
    	
    	List<PsCmd> psCmds = PsCmd.resolves(rec);
    	
    	for(PsCmd cmd : psCmds){
    		System.out.println(JSON.toJSONString(cmd));
    	}*/
    	
    	String path = getPath() + File.separator + CONF_NAME;
    	
    	System.out.println(path);
    	String json = ReadFile(path);
    	
    	
    	JSONObject conf = JSONObject.parseObject(json);
    	
    	
		File file = new File(conf.getString("saveFile"));
		file.createNewFile();
    	FileOutputStream outputStream = new FileOutputStream(file);
    	@SuppressWarnings("resource")
		HSSFWorkbook workbook = new HSSFWorkbook();
    	
    	JSONArray jsonArray = conf.getJSONArray("services");
    	JSONObject service = null;
    	for(int i=0;i<jsonArray.size();i++){
    		try {
    			service = jsonArray.getJSONObject(i);
        		
            	Servicer servicer = new Servicer(service.getString("ip"), service.getString("user")
            			, service.getString("passwd"), service.getInteger("port"), service.getString("remark"));
            	
            	servicer.writeSheet(workbook.createSheet(service.getString("ip") + "_" + i));
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    	
    	/*Servicer servicer = new Servicer("192.168.12.100", "root","*******",22);
    	servicer.writeSheet(workbook.createSheet());*/
    	
    	workbook.write(outputStream);
    	outputStream.flush();
    	outputStream.close();
    }
    
    public static String getPath() {  
        URL url = App.class.getProtectionDomain().getCodeSource().getLocation();  
        String filePath = null;  
        try {  
            filePath = URLDecoder.decode(url.getPath(), "utf-8");
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        if (filePath.endsWith(".jar")) {
            filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);  
        }  
          
        File file = new File(filePath);  
          
        filePath = file.getAbsolutePath();//得到windows下的正确路径  
        return filePath;  
    }
    
	public static String ReadFile(String Path) {
		BufferedReader reader = null;
		String laststr = "";
		try {
			FileInputStream fileInputStream = new FileInputStream(Path);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				laststr += tempString;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return laststr;
	}
}